<template>
  <div class="layout-all" style="width:30%">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="广告名:" prop="title">
        <el-input v-model="form.title" placeholder="请输入广告名" />
      </el-form-item>
      <el-form-item label="广告分类:" prop="cate_id">
        <el-select v-model="form.cate_id" placeholder="请选择广告分类">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="跳转地址:" prop="url">
        <!-- <el-upload
          class="avatar-uploader"
          list-type="picture-card"
          :action="upload_url"
          :show-file-list="false"
          :on-success="handleAvatarSuccess_url"
        >
          <i class="el-icon-plus" />
          <img v-if="url" :src="url" class="avatar">
          <i v-else class="el-icon-plus" />
        </el-upload> -->
        <el-input v-model="form.url" placeholder="请输入跳转地址" />
      </el-form-item>
      <el-form-item label="封面:" prop="thumb">
        <el-upload
          class="avatar-uploader"
          list-type="picture-card"
          :action="upload_url"
          :show-file-list="false"
          :before-upload="$beforeAvatarUpload"
          :on-success="handleAvatarSuccess_thumb"
        >
          <!-- <i class="el-icon-plus" /> -->
          <img v-if="thumb" :src="thumb" class="avatar">
          <i v-else class="el-icon-plus" />
        </el-upload>
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序" type="number" />
      </el-form-item>
      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-close" circle @click="bk()" />
        <el-button
          type="success"
          icon="el-icon-check"
          circle
          @click="onSubmit('form')"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: 0,
      upload_url: '',
      options: [],
      url: '',
      thumb: '',
      form: {
        id: 0,
        sort: 0,
        status: true,
        title: '',
        cate_id: 1,
        url: '',
        thumb: '',
        shop_id: 1
      },
      rules: {
        title: [
          { required: true, message: '请输入广告名', trigger: 'blur' }
        ],
        cate_id: [
          { required: true, message: '请选择广告分类', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
    }
    this.upload_url = process.env.VUE_APP_BASE_API + '/pub/Upload/upload'
    this.init_into()
  },
  methods: {
    // 图片上传完成
    handleAvatarSuccess_url(res, file) {
      this.url = file.response.data.path
      this.form.url = file.response.data.path
    },
    // 封面上传完成
    handleAvatarSuccess_thumb(res, file) {
      this.form.thumb = file.response.data.path
      this.thumb = file.response.data.path
    },
    // 限制图片大小格式
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/admin/Ad/save', form, 'POST').then(e => {})
          this.$router.back()
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      this.get('/admin/Ad/info?id=' + this.id, 'POST').then(e => {
        const tpl = e.info.tpl
        tpl.status === 1 || tpl.status === '' ? this.form.status = true : this.form.status = false
        this.options = e.ad_cate
        this.form.id = tpl.id
        tpl.sort ? (this.form.sort = tpl.sort) : ''
        this.form.title = tpl.title
        this.form.cate_id = tpl.cate_id
        this.form.url = tpl.url
        this.form.thumb = tpl.thumb
        // this.url = tpl.url
        this.thumb = tpl.thumb
        this.form.shop_id = e.shop_id
        console.log(this.form)
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
